package com.bee.littlebeetravel.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.utils.AppManager;
import com.bee.littlebeetravel.utils.ScreenUtil;
import com.bee.littlebeetravel.utils.ToastUtil;
import com.bee.littlebeetravel.utils.ToolsUtil;
import com.example.zhouwei.library.CustomPopWindow;

import net.grandcentrix.tray.AppPreferences;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginIndexActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.rl_main)
    RelativeLayout rl_main;
    @BindView(R.id.tv_next)
    TextView tv_next;

    @OnClick({R.id.iv_clean, R.id.tv_two, R.id.tv_four, R.id.tv_next, R.id.tv_review})
    public void click(View v) {
        Intent intent = new Intent(getApplicationContext(), DescActivity.class);
        switch (v.getId()) {
            case R.id.iv_clean:
                et_phone.setText("");
                break;
            case R.id.tv_two:
                intent.putExtra("title", "用户协议");
                startActivity(intent);
                break;
            case R.id.tv_four:
                intent.putExtra("title", "隐私政策");
                startActivity(intent);
                break;
            case R.id.tv_next:
                Intent intent_vercode = new Intent(getApplicationContext(), VerCodeActivity.class);
                startActivity(intent_vercode);
                break;
            case R.id.tv_review:
                Intent intent_main = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent_main);
                break;

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_index);
        ButterKnife.bind(this);
        AppManager.getInstance().addActivity(this);
        tv_next.setClickable(false);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = s.toString().length();
                //删除数字
                if (count == 0) {
                    if (length == 4) {
                        et_phone.setText(s.subSequence(0, 3));
                    }
                    if (length == 9) {
                        et_phone.setText(s.subSequence(0, 8));
                    }
                }
                //添加数字
                if (count == 1) {
                    if (length == 4) {
                        String part1 = s.subSequence(0, 3).toString();
                        String part2 = s.subSequence(3, length).toString();
                        et_phone.setText(part1 + " " + part2);
                    }
                    if (length == 9) {
                        String part1 = s.subSequence(0, 8).toString();
                        String part2 = s.subSequence(8, length).toString();
                        et_phone.setText(part1 + " " + part2);
                    }
                }

                if (length == 13) {
                    if (ToolsUtil.isPhone(et_phone.getText().toString().replace(" ", ""))) {
                        tv_next.setBackgroundResource(R.drawable.corner_org25);
                        tv_next.setClickable(true);
                    } else {
                        ToastUtil.showShort(getApplicationContext(), "请输入正确的手机号码");
                        tv_next.setClickable(false);
                    }
                } else {
                    tv_next.setBackgroundResource(R.drawable.corner_org25_50);
                    tv_next.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //将光标移动到末尾
                et_phone.setSelection(et_phone.getText().toString().length());
            }
        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AppPreferences appPreferences = new AppPreferences(LoginIndexActivity.this);
                String isShowNotice = appPreferences.getString("isShowNotice", "0");
                if (isShowNotice.equals("0")) {
                    initNotice();
                }
            }
        }, 500);

    }


    // 通知初始化
    public void initNotice() {

        Resources resources = getApplication().getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        int widthPixels = (int) (displayMetrics.widthPixels * 0.8);//当前屏幕宽
        int heightPixels = (int) (displayMetrics.heightPixels * 0.7);//当前屏幕高

        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_notice_login, null);
        TextView tv_no_agree = contentView.findViewById(R.id.tv_no_agree);
        TextView tv_agree = contentView.findViewById(R.id.tv_agree);
        TextView tv_one = contentView.findViewById(R.id.tv_one);
        TextView tv_two = contentView.findViewById(R.id.tv_two);


        final CustomPopWindow customPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .enableOutsideTouchableDissmiss(false)
                .size(widthPixels, heightPixels)//显示大小
                .create()
                .showAtLocation(rl_main, Gravity.CENTER, 0, 0);


        tv_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), DescActivity.class);
                intent.putExtra("title", "用户协议");
                startActivity(intent);
            }
        });

        tv_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), DescActivity.class);
                intent.putExtra("title", "隐私政策");
                startActivity(intent);
            }
        });

        tv_no_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tv_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppPreferences appPreferences = new AppPreferences(LoginIndexActivity.this);
                appPreferences.put("isShowNotice", "1");
                customPopWindow.dissmiss();
            }
        });
    }
}